#导入mysql
import sys

import pymysql
import filter
import  test
from colorama import init
init(autoreset=True)
# 连接数据库
def connect():
    commit=input("\033[32m核对信息后请输入 Y/N: \033[0m")
    if(commit=="Y" or commit=="y"):
        user = input("\033[32mEnter your database root:\033[0m")
        password = input("\033[32mEnter your password: \033[0m")
        db = input("\033[32mChoose or Create your Database: \033[0m")
        return test.dbIsExistCreateAndConnect(user, password, db, "utf8mb4")
    else:
        print("\033[32mBYE BYE\033[0m")
        sys.exit()
def createDBorChoose(conn):

    table=input("\033[32mEnter you Want Create table name: \033[0m")
    create_definitions = ', '.join([f"`{col}` VARCHAR(255)" for col in filter.filterTitle()])
    create_table_sql = f"""
    CREATE TABLE IF NOT EXISTS {table} (
        {create_definitions}
    )
    """
    print("\033[32m创建表中......  \033[0m")
    conn.cursor().execute(create_table_sql)
    print("\033[32m创建表成功...... \033[0m")
    insertArgs= ",".join("`"+col+"`" for col in filter.filterTitle())
    #根据列表的数量来创建%s
    insert_definitions=",".join(["%s" for _ in range(len(filter.filterTitle()))])
    print("\033[32m添加数据中...... \033[0m")
    insert_table_sql=f"""
           insert into {table} ({insertArgs}) values ({insert_definitions})
    """
    print("\033[32m添加数据完成...... \033[0m")
    conn.cursor().executemany(insert_table_sql,filter.filterData())
    # 增/删/改均需要进行commit提交,进行保存
    conn.commit()
    # 关闭游标
    conn.cursor().close()
    # 关闭连接
    conn.close()
    print("\033[32m关闭连接...... \033[0m")
createDBorChoose(connect())